//
//  CZComments.swift
//  GZWeibo666
//
//  Created by mac on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZComments: NSObject {
    /*
    "created_at": "Wed Jun 01 00:50:25 +0800 2011",
    "id": 12438492184,
    "text": "love your work.......",
    "source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
    "mid": "202110601896455629",
    */
    
    var created_at:String?
    
    var id:Int = 0
    
    var text:String?
    
    var source:String?
    
    var mid:String?
    
    var user:CZUser?
    
    
    var rowHeight:CGFloat?
    
    var updateState:Bool = false
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"
        {
            if let dict = value as? [String: AnyObject] {
                // 在这里字典转模型, 赋值给user属性
                user = CZUser(dict: dict)
            }
            
            // 千万要记得return,不然会被父类从新赋值,覆盖掉了
            return
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    class func loadComments(since_id:Int,max_id:Int,id:Int,finished:(results: [CZComments]?, error: NSError?) -> ())
    {
        CZNetworkTool.sharedInstance.loadComments(since_id,max_id: max_id,id: id) { (result, error) -> () in
            if error != nil && result == nil
            {
                print("error = \(error)")
                finished(results: nil, error: error)
                return
            }
            
            if let array = result!["comments"] as? [[String: AnyObject]]
            {
                // 创建一个数组来存放转好的模型
                var comments = [CZComments]()
                
                // 遍历数组,获取里面的每一个字典,
                for dict in array {
                    // 字典转模型
                    let comment = CZComments(dict: dict)
                    comments.append(comment)
                }
                finished(results: comments, error: nil)
            }
            else
            {
                finished(results: nil, error: error)
            }
            
        }
    }
    

}
